﻿namespace bitzhuwei.AnsiCFormat.TestConsole {
    internal class Program {
        static void Main(string[] args) {
            Console.WriteLine("Hello, AnsiCFormat Console!");
            var compiler = new bitzhuwei.AnsiCFormat.CompilerAnsiC();
            var searchPattern = "*";
            var shaderFiles = Directory.GetFiles(@".\..\..\..\c-codes", searchPattern, SearchOption.AllDirectories);
            foreach (var fileName in shaderFiles) {
                var tail = fileName.ToLower();
                if (!(tail.EndsWith(".c"))) {
                    continue;
                }
                Console.WriteLine($"Processing {fileName}");
                var sourceCode = File.ReadAllText(fileName);
                var tokens = compiler.Analyze(sourceCode);
                if (tokens.Error) { throw new Exception(tokens.ToString()); }
                var syntaxTree = compiler.Parse(tokens);
                if (syntaxTree.root == null) { throw new Exception(syntaxTree.ToString()); }
                var translationUnit = compiler.Extract(syntaxTree, tokens, sourceCode);
                ArgumentNullException.ThrowIfNull(translationUnit);
                //using (var writer = new StreamWriter($"{fileName}.format")) {
                //    var context = new FormatContext(tabUnit: 4, tabCount: 0, tokens);
                //    translationUnit.Format(writer, context);
                //}
                //using (var writer = new StreamWriter($"{fileName}.yield")) {
                //    var context = new FormatContext(tabUnit: 4, tabCount: 0, tokens);
                //    foreach (var item in translationUnit.YieldTokens(writer, context)) { }
                //}
                using (var writer = new StreamWriter($"{fileName}.full")) {
                    var context = new FormatContext(tabUnit: 4, tabCount: 0, tokens);
                    translationUnit.FullFormat(new BlankConfig(inlineBlank: 0, forceNewline: false), writer, context);
                }
            }

            Console.WriteLine("done.");
        }
    }
}
